from .Class_SVDObject import *
from xml.dom.minidom import Document as XMLFile

class CPU(SVDObject):
    def __init__(self, name: str,  revision: str = "", endian: str = "", mpuPresent: str = "", fpuPresent: str = "", nvicPrioBits: str = "", vendorSystickConfig: str = "",father : str = ""):
        super().__init__(name)
        self.type = "CPU"
        self.father = father
        self.revision = revision
        self.endian = endian
        self.mpuPresent = mpuPresent
        self.fpuPresent = fpuPresent
        self.nvicPrioBits = nvicPrioBits
        self.vendorSystickConfig = vendorSystickConfig
        self.type = "CPU"
    def __repr__(self):
        return (f"CPU(name={self.name}, type={self.type}, father={self.father}, "
                f"revision={self.revision}, endian={self.endian}, "
                f"mpuPresent={self.mpuPresent}, fpuPresent={self.fpuPresent}, "
                f"nvicPrioBits={self.nvicPrioBits}, vendorSystickConfig={self.vendorSystickConfig})")
    def toString(self, tablevel) -> str:
        xml_output = (
            f"{self.TabsString(tablevel)}<cpu>\n"
            f"{self.TabsString(tablevel + 1)}<name>{self.name}</name>\n"
            f"{self.TabsString(tablevel + 1)}<revision>{self.revision}</revision>\n"
            f"{self.TabsString(tablevel + 1)}<endian>{self.endian}</endian>\n"
            f"{self.TabsString(tablevel + 1)}<mpuPresent>{str(self.mpuPresent).lower()}</mpuPresent>\n"
            f"{self.TabsString(tablevel + 1)}<fpuPresent>{str(self.fpuPresent).lower()}</fpuPresent>\n"
            f"{self.TabsString(tablevel + 1)}<nvicPrioBits>{self.nvicPrioBits}</nvicPrioBits>\n"
            f"{self.TabsString(tablevel + 1)}<vendorSystickConfig>{str(self.vendorSystickConfig).lower()}</vendorSystickConfig>\n"
            f"{self.TabsString(tablevel)}</cpu>\n"
        )
        return xml_output
    def toXmlObj(self, xmlfather):
        cpu_element = xmlfather.ownerDocument.createElement('cpu')
        
        name_element = xmlfather.ownerDocument.createElement('name')
        name_text = xmlfather.ownerDocument.createTextNode(self.name)
        name_element.appendChild(name_text)
        cpu_element.appendChild(name_element)
        
        revision_element = xmlfather.ownerDocument.createElement('revision')
        revision_text = xmlfather.ownerDocument.createTextNode(self.revision)
        revision_element.appendChild(revision_text)
        cpu_element.appendChild(revision_element)
        
        endian_element = xmlfather.ownerDocument.createElement('endian')
        endian_text = xmlfather.ownerDocument.createTextNode(self.endian)
        endian_element.appendChild(endian_text)
        cpu_element.appendChild(endian_element)
        
        mpuPresent_element = xmlfather.ownerDocument.createElement('mpuPresent')
        mpuPresent_text = xmlfather.ownerDocument.createTextNode(str(self.mpuPresent).lower())
        mpuPresent_element.appendChild(mpuPresent_text)
        cpu_element.appendChild(mpuPresent_element)
        
        fpuPresent_element = xmlfather.ownerDocument.createElement('fpuPresent')
        fpuPresent_text = xmlfather.ownerDocument.createTextNode(str(self.fpuPresent).lower())
        fpuPresent_element.appendChild(fpuPresent_text)
        cpu_element.appendChild(fpuPresent_element)
        
        nvicPrioBits_element = xmlfather.ownerDocument.createElement('nvicPrioBits')
        nvicPrioBits_text = xmlfather.ownerDocument.createTextNode(str(self.nvicPrioBits))
        nvicPrioBits_element.appendChild(nvicPrioBits_text)
        cpu_element.appendChild(nvicPrioBits_element)
        
        vendorSystickConfig_element = xmlfather.ownerDocument.createElement('vendorSystickConfig')
        vendorSystickConfig_text = xmlfather.ownerDocument.createTextNode(str(self.vendorSystickConfig).lower())
        vendorSystickConfig_element.appendChild(vendorSystickConfig_text)
        cpu_element.appendChild(vendorSystickConfig_element)
        
        xmlfather.appendChild(cpu_element)
    
    
    def ValidCheck(self):        
        errcnt = super().ValidCheck()
        return errcnt
    
    def SetFather(self, father: str):
        self.father = father
    
    def SetDescription(self, description: str):
        self.description = description

    @staticmethod
    def GetTestObject():
        return CPU("CM0", "r0p0", "little", "False", "False", "2", "False")
        
    @staticmethod
    def TestString():
        testObject = CPU.GetTestObject()
        print(testObject.toString(2))

# 测试代码
if __name__ == "__main__":
    CPU.TestString()